<?php

/**
 * Description of Abstract
 *
 * @author Admin
 */
abstract class Model_Abstract
{
    protected $_table_name = '';


    protected $_db = null;


    public function __construct( $db )
    {
		$this->_db = $db;
		
		$this->_init();
    }
	
	protected function _init()
	{}
    
    public function delete( $id )
    {
		$sql = "DELETE FROM " . $this->_table_name . " WHERE id = $id";

		return $this->_db->get_results($sql);
    }
    
    public function _paramsToString( $params )
    {
		$fields = '';
		foreach( $params as $key => $val )
		{
			if( $val === false )
			continue;

			if( $fields != '' )
			$fields .= ', ';

			$fields .= $key . "=";

			$_is_sql_param = false;
			if( !empty( $val[0] ) && $val[0] == ':' )
			{
			$val = mb_substr( $val, 1 );
			$_is_sql_param = true;
			}

			if( !is_numeric( $val ) && !$_is_sql_param )
			$fields .= "'";

			$fields .= $val;

			if( !is_numeric( $val ) && !$_is_sql_param )
			$fields .= "'";
		}

		return $fields;
    }
    
    public function _glueKeyValue( $params )
    {
		$fields = '';
		foreach( $params as $key => $val )
		{
			$fields .= $key;

			if( !empty( $val[0] ) && $val[0] == ':' )
			{
				$val = mb_substr( $val, 1 );
				$_is_sql_param = true;
			}

			if( !is_numeric( $val ) && !$_is_sql_param )
				$fields .= "'";

			$fields .= $val;

			if( !is_numeric( $val ) && !$_is_sql_param )
				$fields .= "'";
		}

		return $fields;
    }
    
    protected function _getLastInsertId()
    {
		$sql = "SELECT LAST_INSERT_ID() FROM" . $this->_table_name;
		$link_id = $this->_db->get_col($sql);
		if( count( $link_id ) )
			return current( $link_id );

		return false;
    }
	
	public function translit( $text )
    {
		$tr = array(
			"Ґ"=>"G","Ё"=>"YO","Є"=>"E","Ї"=>"YI","І"=>"I",
			"і"=>"i","ґ"=>"g","ё"=>"yo","№"=>"#","є"=>"e",
			"ї"=>"yi","А"=>"A","Б"=>"B","В"=>"V","Г"=>"G",
			"Д"=>"D","Е"=>"E","Ж"=>"ZH","З"=>"Z","И"=>"I",
			"Й"=>"Y","К"=>"K","Л"=>"L","М"=>"M","Н"=>"N",
			"О"=>"O","П"=>"P","Р"=>"R","С"=>"S","Т"=>"T",
			"У"=>"U","Ф"=>"F","Х"=>"H","Ц"=>"TS","Ч"=>"CH",
			"Ш"=>"SH","Щ"=>"SCH","Ъ"=>"'","Ы"=>"YI","Ь"=>"",
			"Э"=>"E","Ю"=>"YU","Я"=>"YA","а"=>"a","б"=>"b",
			"в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"zh",
			"з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
			"м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
			"с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
			"ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"'",
			"ы"=>"yi","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya", '+'=>'-plus-',
			' '=>'-', ','=>'-', ';'=>'-', ':'=>'-', '"'=>'-', '&'=>'',
			'.'=>'-', '!'=>'-', '@'=>'-', '#'=>'-', '$'=>'-', '&quote;'=>'',
			'%'=>'-', '^'=>'-', '*'=>'-', '('=>'-', ')'=>'-', '&#39;'=>'',
			'~'=>'-', '`'=>'-', "'"=>'-', '['=>'-', ']'=>'-', 
			'{'=>'-', '}'=>'-', '\\'=>'-', '|'=>'-', '/'=>'-'
		);

		return strtoupper( strtr( $text, $tr ) );
    }
    
}
?>
